/*
*  通用状态管理
*/
import API from '../../api';
// import { asyncRouterMap, constantRouterMap } from '@/router';
const SET_LOGIN_SATEUS = 'SET_LOGIN_SATEUS'; // 登录窗口判断
const SET_PAY_PASSWORD = 'SET_PAY_PASSWORD'; // 支付密码判断
const SET_USER_INFO = 'SET_USER_INFO'; // 设置用户信息
const SET_SCROLL_TOP = 'SET_SCROLL_TOP'; // 设置滚动距离
const SET_ISTAB_BAR = 'SET_ISTAB_BAR'; // 设置滚动距离
const SET_EXIT_LOGIN = 'SET_EXIT_LOGIN'; // 退出登录
const SET_IS_CERT = 'SET_IS_CERT'; // 判断实名认证
const SET_IS_LOGIN_TYPE = 'SET_IS_LOGIN_TYPE'; // 登录状态
const SET_ROUTER_INDEX = 'SET_ROUTER_INDEX'; // form中index
// const SET_ROUTER = 'SET_ROUTER'; // 路由
const state = {
  isLogin: false,
  isPayPassword: false,
  userInfo: {},
  scrollTop: [],
  isCert: false,
  loginType: 'login',
  isTabBar: false,
  routers: [],
  routerIndex: {
    toIndex: 2,
    formIndex: 1
  }
};
const getters = {
  routers: state => state.routers,
  routerIndex: state => state.routerIndex,
  loginType: state => state.loginType,
  isLogin: state => state.isLogin,
  userInfo: state => state.userInfo,
  isCert: state => state.isCert,
  scrollTop: state => state.scrollTop,
  isPayPassword: state => state.isPayPassword,
  isTabBar: state => state.isTabBar
};
const actions = {
  setLogin ({ commit }, data) {
    commit(SET_LOGIN_SATEUS, data);
  },
  setRouterIndex ({ commit }, data) {
    commit(SET_ROUTER_INDEX, data);
  },
  // setRouter ({ commit }) {
  //   return new Promise(resolve => {
  //     commit(SET_ROUTER, asyncRouterMap);
  //     resolve();
  //   });
  // },
  setLoginType ({ commit }, data) {
    commit(SET_IS_LOGIN_TYPE, data);
  },
  exitLogin ({ commit }, data) {
    commit(SET_EXIT_LOGIN, data);
  },
  setTabbar ({ commit }, data) {
    commit(SET_ISTAB_BAR, data);
  },
  setPayPassword ({ commit }, data) {
    commit(SET_PAY_PASSWORD, data);
  },
  setIsCert ({ commit }, data) {
    commit(SET_IS_CERT, data);
  },
  setScrollTop ({ commit }, data) {
    commit(SET_SCROLL_TOP, data);
  },
  setUserInfo ({ commit }, data) {
    API.user.getUserInfo().then(res => {
      commit(SET_USER_INFO, res.data);
      commit(SET_IS_CERT, !res.data.isCert);
    });
  }
};

const mutations = {
  [SET_ISTAB_BAR] (state, data) {
    state.isTabBar = data;
  },
  [SET_ROUTER_INDEX] (state, data) {
    state.routerIndex = data;
  },
  // [SET_ROUTER] (state, data) {
  //   state.routers = data.concat(constantRouterMap);
  // },
  [SET_IS_LOGIN_TYPE] (state, data) {
    state.loginType = data;
  },
  [SET_IS_CERT] (state, data) {
    state.isCert = data;
  },
  [SET_EXIT_LOGIN] (state, data) {
    state.isLogin = false;
    state.isPayPassword = false;
    state.userInfo = {};
    state.scrollTop = [];
    state.isTabBar = false;
  },
  [SET_LOGIN_SATEUS] (state, data) {
    state.isLogin = data;
  },
  [SET_SCROLL_TOP] (state, data) {
    state.scrollTop = data;
  },
  [SET_PAY_PASSWORD] (state, data) {
    state.isPayPassword = data;
  },
  [SET_USER_INFO] (state, data) {
    state.userInfo = data;
  }
};

export default {
  state,
  getters,
  actions,
  mutations
};
